<script src="<?php echo $url_styles ?>js/plugins/formselements/jquery.maskedinput-1.3.min.js"></script>
<script src="<?php echo $url_styles ?>js/plugins/formselements/ui.spinner.min.js"></script>
<script src="<?php echo $url_styles ?>js/plugins/gallery/jquery.colorbox-min.js"></script>
<script>
	var page=1;
    $(document).ready(function(){
        var data =<?php echo json_encode($data) ?>;
		for(var i in data){
			//			alert(i);
			//			alert(data[i]);
			$('.form-horizontal [name="'+i+'"]').attr('value',data[i]);
		}
        var error = <?php echo json_encode($error) ?>;
        for(var i in error)
        {
            if(error[i]!=''){
                $('.form-horizontal [name="'+i+'"]').after('<span class="help-inline">'+error[i]+'</span>');
                $('.form-horizontal [name="'+i+'"]').parent().parent().addClass('error');
            }

        }
		
		$('#selectfile').uploadInit({
			url: '<?= url_create('admin', 'file', 'upload') ?>',
			multi_selection: false,
			imageContainer: $('#wm_overlay_path'),
			imageVar: 'overlay_id'
		})
    });
	function load_table()
	{
		$.ajax({
			url: '<?php echo url_create('admin', 'limit', 'read') ?>',
			dataType: 'json',
			type: 'post',
			data: {p: page},
			success: function(o)
			{
				if(o.state=='success'){
					var str='';
					for(var i in o.data){
						var r=o.data[i];
						var row_class = (o.data[i].suppend==1)?'X':'A';
						if(i%2==1) row_class+=" odd";
						else row_class+=" even";
						str+='<tr id="row_'+r.id+'" class="grade'+row_class+'">'+
							'<td>'+r.likes+'</td>'+
							'<td>'+r.images_max+'</td>'+
							'<td>'+r.videos_max+'</td>'+
							'<td class="center"><a href="#user_modal" data="'+r.id+'" class="btn btn-mini btn-success row_edit"><?php echo lang('edit') ?></a>&nbsp;<button class="btn btn-mini btn-danger row_delete" data="'+r.id+'"><?php echo lang('delete') ?></button></td>'+
							'</tr>';
					}
					if(typeof(oTable)!='undefined') oTable.fnDestroy();
					$('#table tbody').html(str);
					var p = o.pagination;
					var pnumbers = p.left?'...':'';
					for(var i in p.pnumbers){
						var clas = p.pnumbers[i]==p.curr?'active':'button';
						pnumbers+='<a data="'+p.pnumbers[i]+'" class="paginate_'+clas+'">'+p.pnumbers[i]+'</a>';
					}
					if(p.right) pnumbers+='...';
					$('#table_paginate span').html(pnumbers);
					$('#table_previous').attr('data',p.prev);
					$('#table_next').attr('data',p.next);
					$('#table_first').attr('data',p.first);
					$('#table_last').attr('data',p.last);

				}
			}
		});
	}
	$('#table_paginate a').live('click',function(e){
		var p = $(this).attr('data');
		if(p!=null){
			page=p;
			load_table();
		}
	});
	$('.row_delete').live('click',function(e){
		if(confirm('<?php echo lang('delete_confirm') ?>'))
		{
			$.ajax({
				url: '<?php echo url_create('admin', 'limit', 'delete') ?>',
				type: 'post' ,
				dataType: 'json',
				data:{id: $(this).attr('data')},
				success:function(o)
				{
					if(o.state == 'success')
						$('#row_'+o.id).fadeOut(500);
					else
						$.jGrowl(o.message, {
							header: o.title,
							theme: "notification_styled_"+o.state 
						});
				}
			});
		}
		return false;
	});
        
	$('.row_edit').live('click',function(e){
		$('#modal').attr('flag','edit');
		$('#modal').attr('data_id',$(this).attr('data'));
		$.ajax({
			url: '<?php echo url_create('admin', 'limit', 'detail') ?>',
			dataType: 'json',
			data: {id: $(this).attr('data')},
			success: function(o){
				if(o.state=='success'){
                        
					// Ghi du lieu
					$('#likes').attr('value',o.data.likes);
					$('#images_max').attr('value',o.data.images_max);
					$('#videos_max').val(o.data.videos_max);
						
					// Gan lai tieu de 
					$('#modal_title').html('<?php echo lang('limit_update') ?>');
					$('#modal_toggle').click();
						
                        
				}
				else
					$.jGrowl(o.message, {
						header: o.title,
						theme:"notification_styled_"+o.state
					});
			}
		})
	});
        
	$('.data_create').live('click',function(e){
		$('#modal').attr('flag','create');		
		// Reset field
		$('#likes').attr('value','0');
		$('#images_max').attr('value','0');
		$('#videos_max').val('0');
		// Gan tieu de modal
		$('#modal_title').html('<?php echo lang('limit_create') ?>');
		$('#modal_toggle').click();
	});
		
	$('.data_update').live('click',function(e){
		$.ajax({
			url: '<?php echo url_create('admin', 'limit', 'update_all') ?>',
			dataType: 'json',
			success: function(o){
				if(o.state=='success'){
					load_table();
					$.jGrowl(o.message, {
						header: o.title,
						theme:"notification_styled_"+o.state 
					});
				}
				else{
					alert(o.message);
				}
						
			}
		})
	})
	$('#modal_ok').live('click',function(e){
		$('.modal-form').submit();
	});
	$('.modal-form').submit(function(e){
		var flag = $('#modal').attr('flag');

		$.ajax({
			url: flag=='create'?'<?php echo url_create('admin', 'limit', 'create') ?>':'<?php echo url_create('admin', 'limit', 'update') ?>',
			dataType: 'json',
			type: 'post',
			data: {
				id: $('#modal').attr('data_id'),
				likes: $('#likes').attr('value'),
				videos_max: $('#videos_max').attr('value'),
				images_max: $('#images_max').val()
			},
			success: function(o){
				if(o.state=='success'){
					load_table();
					$('#modal_toggle').click();
					$.jGrowl(o.message, {
						header: o.title,
						theme:"notification_styled_"+o.state 
					});
				}else{
					alert(o.message);
				}
					
			}
		});
		return false;
	});
		
	$('#modal_cancel').live('click',function(e){
		$('#modal_toggle').click();
		return false;
	});
	$(document).ready(load_table());
</script>
